/**
 * Copyright © 2009 Qasky. All rights reserved.
 *
 * 封装 axios 进行 http 请求
 *
 * @author Wang Hongya
 * @version 0.0.0 2019/10/14
 *
 * History:
 * (author, date, desc)
 * (Wang Hongya, 20191014, 创建文件)
 */
import axios from 'axios';
import {message} from 'ant-design-vue';
const {  name  } = require('../../package.json')
/* 创建axios实例 */
const request = axios.create({
    timeout: 15 * 60 * 1000 // 请求超时时间
});
let subMicro = sessionStorage.getItem('subMicro')
let version = ''
try{
    let subMicroItem = JSON.parse(subMicro)
    let target =subMicroItem.filter(item=>item.serverNameAndWebVersion === name)
    if(target.length >0){
        version =  target[0].version
    }
}catch (e) {

}
// request拦截器
request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=UTF-8';
    let regApi = /^(api\/)/ig;
    config.url = config.url.replace(regApi, '');
    config.url='/sysconf/'+config.url;
    config.headers['Authorization'] = "Bearer "+sessionStorage.getItem('token');
    config.headers['version'] = version;
    if (config.url) {
        if (config.url.indexOf('?') < 0) {
            config.url = config.url + '?t=' + new Date().getTime();
        } else {
            config.url = config.url + '&t=' + new Date().getTime();
        }
    }
    return config;
}, error => {
    Promise.reject(error);
});

/*
 * response拦截器,就是请求发送回来后对一些配置进行操作
 * */
request.interceptors.response.use(
    response => {
        if (response && response.data && response.data.code &&
            (response.data.code === 402 || response.data.code === 502
                || response.data.code === 404
                || response.data.code === 405 || response.data.code === 406
                || response.data.code === 407 || response.data.code === 408)) {

            /*
             * 当前认证登录方式是否UKey身份鉴别
             */
            let currentLoginType = sessionStorage.getItem('currentLoginType');
            if (response.data.code !== 405 && currentLoginType === 'UKEY') {
                axios.get('http://127.0.0.1:18801/ukeyauth/logoutDev');
            }

            sessionStorage.clear();
            sessionStorage.setItem('interceptorMsg',response.data.message);
            window.location.reload();
        }
        // 请求正常则返回
        return Promise.resolve(response);
    },
    error => {
        if (error.code === 'ECONNABORTED' &&
            error.message.indexOf('timeout') !== -1) {
            message.error('请求超时，请重新请求');
            return error;
        }

        if (error.response) {
            switch (parseInt(error.response.status)) {
                case 403:
                    message.info({
                        content: '您没有相关权限访问该页数据',
                        duration: 10,
                        closable: true,
                    });
                    break;
                case 404:
                    /**
                     * 当前认证登录方式是否UKey身份鉴别
                     **/
                    let currentLoginType = sessionStorage.getItem('currentLoginType');
                    if (currentLoginType === 'UKEY') {
                        axios.get('http://127.0.0.1:18801/ukeyauth/logoutDev');
                    }

                    sessionStorage.clear();
                    sessionStorage.setItem('interceptorMsg','会话失效，请重新登录');
                    window.location.reload();
                    break;
                case 500:
                    if(error.response.config.url.indexOf('/dynamicRouter') !== -1
                        || error.response.config.url.indexOf('/login.jsp') !== -1){
                        /**
                         * 当前认证登录方式是否UKey身份鉴别
                         **/
                        let currentLoginType = sessionStorage.getItem('currentLoginType');
                        if (currentLoginType === 'UKEY') {
                            axios.get('http://127.0.0.1:18801/ukeyauth/logoutDev');
                        }

                        sessionStorage.clear ();
                        sessionStorage.setItem ('interceptorMsg', '服务器出错了，数据请求失败');
                        window.location.reload()
                    } else {
                        message.error('服务器出错了,数据请求失败');
                    }
                    break;
                case 503:
                    message.error('服务器出错了,数据请求失败');
                    break;
                case 504:
                    message.error('服务器出错了,数据请求失败');
                    break;
                default:
                    break;
            }
        }
        return Promise.reject(error);
    },
);

export default request;
